﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using RuanRang.Media.Core;
using RuanRang.Media.Core.Common;
using RuanRang.Media.Core.Internal;
using RuanRang.Media.Core.Io;
using RuanRang.Media.Core.Media;
using RuanRang.Media.MediaInfo;

namespace VLCPlayTool
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool SetDllDirectory(string lpPathName);
        public Form1()
        {
            InitializeComponent();
            //var setVlc = SetDllDirectory(@"H:\TFS_My Work\CodePlex\Costoolsuite\Bin\VLC\x86\2.0.5");
            //if (!setVlc)
            //{
            //    throw new Exception("未找到VLC插件目录");
            //}
            //var setMediaInfo = SetDllDirectory(@"H:\TFS_My Work\CodePlex\Costoolsuite\Bin\MediaInfo\x86\0.7.61.0");
            //if (!setMediaInfo)
            //{
            //    throw new Exception("未找到MediaInfo插件目录");
            //}

            if (!vlcPlayerControl1.IsInitialized)
            {
                vlcPlayerControl1.Initialize(this);
            }

            var list = Enum.GetNames(typeof(VideoAspectRatioType));
            foreach (var s in list)
            {
                comboBox1.Items.Add(s);
            }
        }
        /// <summary>
        /// Gets the media.
        /// </summary>
        /// <value>The media.</value>
        public MediaInput Media { get; private set; }
        public BasicVideoInformation VideoInformation { get; private set; }

        /// <summary>
        /// Handles the Click event of the button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        /// <exception cref="System.Exception">不是有效的视频或音频文件。</exception>
        private void button1_Click(object sender, EventArgs e)
        {
            //const string vPath = @"G:\TDDOWNLOAD\CB2012.MP4";
            //textBox1.Text = vPath;
            var vPath = textBox1.Text;
            Media = new MediaInput(MediaInputType.NetworkStream, vPath);
            //Media = new MediaInput(MediaInputType.NetworkStream, "http://127.0.0.1:8811/CB2012.MP4");
            //using (var mediaInfoLibrary = new MediaInfoLibrary())
            //{
            //    VideoInformation = MediaInfoHelper.GetBasicVideoInfo(mediaInfoLibrary, vPath);
            //    if (String.IsNullOrEmpty(VideoInformation.VideoCodec) && String.IsNullOrEmpty(VideoInformation.AudioCodec))
            //    {
            //        throw new Exception("不是有效的视频或音频文件。");
            //    }
            //}
            //var mediaInfo = vlcPlayerControl1.Player.ParseMediaInput(Media);
            vlcPlayerControl1.MediaPlayerPositionChanged += (player, args) =>
                {
                    label1.Text = (args.Data*100).ToString("000") + " %";
                    var ps = (int) (args.Data*100);
                    if (trackBar1 != null && ps != trackBar1.Value)
                    {
                        trackBar1.Value = ps;
                    }
                };
            vlcPlayerControl1.MediaPlayerStateChanged += (player, args) =>
                {
                    var state = args.Data;
                    label2.Text = state.ToString();
                    switch (state)
                    {
                        case VlcPlayerState.Idle:
                            trackBar1.Value = 0;
                            label1.Text = "000";
                            break;
                    }
                };
            vlcPlayerControl1.Play(Media);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            vlcPlayerControl1.Volume = 200;
            vlcPlayerControl1.Player.IsFullScreen = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            vlcPlayerControl1.Stop();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var vlcPlayer = vlcPlayerControl1.Player as VlcMediaPlayer;
            if (vlcPlayer == null) return;
            vlcPlayer.SetVideoAdjust(VideoAdjustOption.Enable, 1);

            vlcPlayer.SetVideoAdjust(VideoAdjustOption.Brightness, float.Parse(textBox2.Text));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var vlcPlayer = vlcPlayerControl1.Player;
            if (vlcPlayer == null) return;
            uint width, height;
            vlcPlayer.GetVideoSize(out width, out height);
            Width = (int)width;
            Height = (int)height + panel1.Height;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var vlcPlayer = vlcPlayerControl1.Player;
            if (vlcPlayer == null) return;
            var type = (VideoAspectRatioType)Enum.Parse(typeof(VideoAspectRatioType), comboBox1.SelectedItem.ToString());
            vlcPlayer.SetvideoAspectRatio(type);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var vlcPlayer = vlcPlayerControl1.Player;
            if (vlcPlayer == null) return;
            MessageBox.Show(vlcPlayer.SPU.ToString());
        }

        private void button7_Click(object sender, EventArgs e)
        {
            var vlcPlayer = vlcPlayerControl1.Player;
            if (vlcPlayer == null) return;
            //TODO logo一闪而过，但是设置启用，在禁用再启用之后又恢复？WHY？ 
            vlcPlayerControl1.Player.SetVideoLogo(VideoLogoOption.X, 10);
            vlcPlayerControl1.Player.SetVideoLogo(VideoLogoOption.Y, 10);
            vlcPlayerControl1.Player.SetVideoLogo(VideoLogoOption.File, textBox3.Text);
            vlcPlayer.SetVideoLogo(VideoLogoOption.Enable, 1);

        }

        private void button8_Click(object sender, EventArgs e)
        {
            var vlcPlayer = vlcPlayerControl1.Player;
            if (vlcPlayer == null) return;
            vlcPlayer.AddMediaOption(textBox4.Text);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            var vlcPlayer = vlcPlayerControl1.Player;
            if (vlcPlayer == null) return;
            vlcPlayer.SetVideoLogo(VideoLogoOption.Enable, 0);
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            var vlcPlayer = vlcPlayerControl1.Player;
            if (vlcPlayer == null) return;
            if (trackBar1 != null)
            {
                float val = 1f * trackBar1.Value / trackBar1.Maximum;
                if (val != vlcPlayer.Position)
                {
                    vlcPlayer.Position = val;
                }
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            vlcPlayerControl1.PauseOrResume();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Process.GetCurrentProcess().Kill();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            var media = new PathMedia(new MediaInput(MediaInputType.File, textBox1.Text));
            MessageBox.Show(media.Duration.TotalMilliseconds.ToString());
        }

        private void button12_Click(object sender, EventArgs e)
        {
            using(var od = new OpenFileDialog())
            {
                if (od.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }
                textBox1.Text = od.FileName;
            }
        }
    }
}
